package com.easyfun.healthmagicbox.base;


import java.io.File;
import java.io.FileNotFoundException;

import com.easyfun.healthmagicbox.R;


import com.easyfun.healthmagicbox.util.URIConverter;

import eu.janmuller.android.simplecropimage.CropImage;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;

public abstract class PicturePickerBaseActivity extends BaseActivity{
	public static final int TAKE_PICTURE = 0;
	public static final int CHOOSE_PICTURE = 1;
	public static final int CROP_PHOTO_RESULT = 3;
	protected static final String TAKE_PHOTE_TEMP_FILENAME = "temp.jpg";
	protected static String IMG_PATH = "";
	public static Uri TAKE_PHOTE_TEMP_URI ;//= Uri.fromFile(new File(IMG_PATH, TAKE_PHOTE_TEMP_FILENAME));
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMG_PATH = getStorePath() + java.io.File.separator + "healthbox";
		TAKE_PHOTE_TEMP_URI = Uri.fromFile(new File(IMG_PATH, TAKE_PHOTE_TEMP_FILENAME));
		
		File path = new File(IMG_PATH);
		if (!path.exists()) {
			path.mkdirs();
		}
		
	}
	
	abstract protected String getImageFileName(String m_personID2);
	
	public void showPicturePicker(Context context){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("选择图片来源");
		builder.setNegativeButton("OK", null);
		builder.setItems(new String[]{"拍照","同图片库中选择"}, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case TAKE_PICTURE:
					Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, TAKE_PHOTE_TEMP_URI);
					startActivityForResult(openCameraIntent, TAKE_PICTURE);
					break;
					
				case CHOOSE_PICTURE:
										
					Intent openAlbumIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
					
					//openAlbumIntent.addCategory(Intent.CATEGORY_OPENABLE);
					openAlbumIntent.setType("image/*");

					startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
					
					break;

				default:
					break;
				}
			}
		});
		builder.create().show();
	}
	

	/**
	 * before get SD card, detect SD card first
	 * 
	 * @return 
	 */
	public String getStorePath() {
		File sdDir = null;
		
		if (URIConverter.checkSDCardAvailable()) {
			sdDir = Environment.getExternalStorageDirectory();// ��ȡ���Ŀ¼
		}else
		{
			sdDir = this.getFilesDir();
		}
		return sdDir.toString();
	}
	
//  Following code doesn't work on my XT912
	
//	Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//	intent.addCategory(Intent.CATEGORY_OPENABLE);
//	intent.setType("image/*");
//	intent.putExtra("crop", "true");
//	intent.putExtra("scale", true);
//	intent.putExtra("return-data", false);
//	intent.putExtra(MediaStore.EXTRA_OUTPUT,
//			Uri.fromFile(new File(IMG_PATH, "temp_cropped.jpg")));
//	intent.putExtra("outputFormat",
//			Bitmap.CompressFormat.JPEG.toString());
//	intent.putExtra("noFaceDetection", true); // no face detection
//	startActivityForResult(intent, PHOTO_RESULT);
	
	/**
	 * 
	 */
	public void startPhotoCrop(Uri uri, String outputFilename) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
		intent.putExtra("scale", true);
		intent.putExtra("outputX", 230);
		intent.putExtra("outputY", 230);
	        
		intent.putExtra("return-data", false);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true); // no face detection
		
		intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(
				new File(IMG_PATH,outputFilename)));
		startActivityForResult(intent, CROP_PHOTO_RESULT);
	}
	
	
	public void startCropWith3rd(Uri uri, String outputFilename,boolean isCircle,String format, int size_X, int size_Y)
	{
		
		 // create explicit intent
	    Intent intent = new Intent(this, CropImage.class);

	    // tell CropImage activity to look for image to crop 
	    intent.putExtra(CropImage.IMAGE_PATH,IMG_PATH + java.io.File.separator + TAKE_PHOTE_TEMP_FILENAME);
	    intent.putExtra(CropImage.OUTPUT_IMAGE_PATH, IMG_PATH + java.io.File.separator + outputFilename);
	    // allow CropImage activity to rescale image
	    intent.putExtra(CropImage.SCALE, true);

	    // if the aspect ratio is fixed to ratio 3/2
	    intent.putExtra(CropImage.ASPECT_X, 1);
	    intent.putExtra(CropImage.ASPECT_Y, 1);
	    intent.putExtra(CropImage.OUTPUT_X, size_X);
	    intent.putExtra(CropImage.OUTPUT_Y, size_Y);
	    if (isCircle)
	    {
	    	intent.putExtra(CropImage.CIRCLE_CROP, "true");
	    }
	    intent.putExtra(CropImage.RETURN_DATA, false);
	    intent.putExtra(CropImage.OUTPUT_FORMAT, format);

	    // start activity CropImage with certain request code and listen
	    // for result
	    startActivityForResult(intent, CROP_PHOTO_RESULT);
	    /* */
	    
	}
	/* */
	
	
	/**
	 * 
	 * 
	 * @param uri
	 * @return
	 */
	protected Bitmap decodeUriAsBitmap(Uri uri, int zoominscale) {
		Bitmap bitmap = null;
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inSampleSize = zoominscale;//图片宽高都为原来的二分之一，即图片为原来的四分之一
		try {
			bitmap = BitmapFactory.decodeStream(getContentResolver()
					.openInputStream(uri),null, options);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		return bitmap;
	}
}
